import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

import login from './routes/login'
import admin from './routes/admin'
import common from './routes/common'

Vue.use(VueRouter)

const routes = [
  login,
  admin,
  ...common
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 不是登录页面
  if (to.path !== '/login') {
    if (store.getters.token === '') {
      next({ path: '/login', replace: true })
    } else {
      next()
    }
  } else {
    next()
  }
})


export default router
